package com.iroc.springboot.controller;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.iroc.springboot.util.DecryptUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class LoginController
{

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam(value = "jsonReqParams") String jsonReqParams){
        JsonObject result = new JsonObject();
        //设数据库中username为zhangsan，password为123456
        try
        {
            //解密前端请求的报文
            jsonReqParams = DecryptUtil.decrypt(jsonReqParams);
            JsonObject jsonObject = new JsonParser().parse(jsonReqParams).getAsJsonObject();
            String username = jsonObject.get("username").getAsString();
            String password = jsonObject.get("password").getAsString();
            if("zhangsan".equals(username)&&"123456".equals(password)){
                result.addProperty("code","1001");
                result.addProperty("desc","登录成功");
            }else {
                result.addProperty("code","1002");
                result.addProperty("desc","账号或密码有误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.addProperty("code","1003");
            result.addProperty("desc","出现异常，请联系管理员");
        }

        return result.toString();
    }


}
